<html>
  <head>
    <title>函数式组件props</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../../js/babel.min.js"></script>
    <script type="text/javascript" src="../../js/prop-types.js"></script>
    <script type="text/babel">
      function Person(props) {
        const { name, age, sex, speak } = props;

        return (
          <ul onClick={() => speak(name)}>
            <li>姓名：{name}</li>
            <li>年龄：{age}</li>
            <li>性别：{sex}</li>
          </ul>
        );
      }

      Person.propTypes = {
        name: PropTypes.string.isRequired,
        age: PropTypes.number,
        sex: PropTypes.oneOf(['男', '女']),
        speak: PropTypes.func,
      }

      Person.defaultProps = {
        age: 0,
        sex: '不男不女',
        speak: () => {},
      };

      class Main extends React.Component {
        render() {
          const speak = (name) => {
            console.log(`Hello, My name is ${name}`);
          };

          const persons = [
            { name: 'Judith', age: 18, sex: '女', speak },
            { name: 'ZhangSan', sex: '男', speak },
          ];

          return (
            <>
              {
                persons.map((person, index) => <Person {...person} key={index} />)
              }
            </>
          );
        }
      }
      ReactDOM.render(<Main />, document.getElementById('root'));
    </script>
  </body>
</html>
